package com.nondo.dean.platform.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.model.result.ProductResult;

/**
 * 产品管理业务类
 * 
 * @author zhaoljin
 * @date 2018年4月3日
 */
@Component
public class ProductService {

    @Autowired
    RestTemplate restTemplate;

    /**
     * 根据产品ID获取产品信息
     * 
     * @param productId 产品ID
     * @return 产品信息{@link ProductResult}
     */
    public ProductResult getProductBeanById(Integer productId) {
        ProductResult productResult = new ProductResult();
        @SuppressWarnings("unchecked")
        View<LinkedHashMap<String, Object>> result =
            restTemplate.postForEntity("http://dean-product/product/v1/inner/product/result/" + productId, null, View.class)
                .getBody();
        if (result.getCode() == 20000 && result.getData() != null) {
            BeanUtils.copy(result.getData(), productResult);
        }
        return productResult;
    }

    /**
     * 根据多个产品ID值获取产品列表
     * @param ids 多个产品ID值
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<ProductResult> getProductBeanListByIds(String ids) {
        List<ProductResult> productResultList = new ArrayList<ProductResult>();
        if (StringUtils.isNotBlank(ids)) {
            View<LinkedHashMap<String, Object>> result =
                restTemplate.postForEntity("http://dean-product/product/v1/inner/product/list/" + ids, null, View.class)
                    .getBody();
            if (result.getCode() == 20000 && result.getData() != null) {
                productResultList = (List<ProductResult>)result.getData();
            }
        }
        return productResultList;
    }

}
